$(document).ready(function() {

	// 分页查询，异步提交
	function doAjax(d) {

		$.ajax({
					type : "post",
					url : "lzw/taskTrack_queryMyTasks.action",
					data : "querypage=" + getParams(d),
					dataType : "JSON",
					success : showPage,
					error : function(textStatus) {
						alert("Ajax请求失败，请检查本地的网络连接！");
						return false;
					}
				});

	}

	function getParams(p) {

		$("#taskList").empty();
		$("#loadingTask").show();
		return p;
	}
	
	$.ajax({
				type : "post",
				url : "lzw/taskTrack_queryMyTasks.action",
				data : "querypage=" + getParams(1),
				dataType : "JSON",
				success : showPage,
				error : function(textStatus) {
					alert("Ajax请求失败，请检查本地的网络连接！");
					return false;
				}
			});

	// 分页查询，查第一页
	$("#firstPage").click(function() {
				doAjax(getParams(1));
			});
	// 分页查询，查上一页
	$("#prePage").click(function() {
				var pageCount = $("#pageCount").text();
				var page = $("#page").text();
				page = (parseInt(page) - 1);
				if (page <= 0) {
					alert("当前已是第一页！");
					return false;
				}
				doAjax(getParams(page));
			});
	// 分页查询，查下一页
	$("#nextPage").click(function() {
				var pageCount = $("#pageCount").text();
				var page = $("#page").text();
				page = (parseInt(page) + 1);
				if (page > pageCount) {
					alert("当前已是末页！");
					return false;
				}
				doAjax(getParams(page));
			});
	// 分页查询，查最后一页
	$("#lastPage").click(function() {
				var page = $("#pageCount").text();
				doAjax(getParams(page));
			});
	// 分页查询，跳转到第n页
	$("#jumpPage").click(function() {
				var pageCount = $("#pageCount").text();
				var page = $("#jump").val();
				if (page > pageCount) {
					alert("页数越界！");
					$("#pageCount").text(0);
					return false;
				}
				doAjax(getParams(page));
			});

	function showPage(data) {

		$("#taskList").empty();
		var d = eval("(" + data + ")");
		if (d.userTasks == null && d.errMsg != null) {
			$("#loadingTask").hide();
			$("#noneTask").show();
		    $("#total").text(0);
		    $("#page").text(0);
		    $("#pageCount").text(0);
			alert(d.errMsg);
		} else {
			$("#taskList").hide();
			$("#noneTask").hide();
			$("#loadingTask").hide();
			
			$.each(d.userTasks.list, function(i, n) {
						fillDataToHtml(n.EId, n.id, n.name, n.time, n.dept,n.phone, n.status);
					});
					
		    $("#total").text(d.userTasks.total);
		    $("#page").text(d.userTasks.page);
		    $("#pageCount").text(Math.ceil(d.userTasks.total / 10));
			$("#taskList").show();
		}

	}

	function fillDataToHtml(EId, id, name, time, dept, phone, status) {

		var strHtml = "<tr>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ "<input name='checkbox' type='button' value='查看'id='"
				+ $.trim(id) // 去除id字符串的空格
				+ "'/></div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ EId + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ id + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ name + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ time + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ status + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ dept + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ phone + "</div></td>";
		strHtml += "</tr>";
		$("#taskList").append(strHtml);
		var id_value = "#" + $.trim(id);// 去除id字符串的空格
		$(id_value).click(function() {
		    var date = new Date(); 
	        date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000)); 
	        //$.cookie(cookieName, cookieValue, { path: '/', expires: date });
			$.cookie('tasktrackid', $(this).attr("id"), { path: '/', expires: date });
			$.cookie('taskEventId', EId);
			window.location.href="/ag/lzw/task_main.jsp";
		});
	}
});
